package Utils;

import com.sun.jmx.snmp.internal.SnmpMsgProcessingModel;

import java.util.Random;

import static java.lang.Math.pow;

/**
 * @Author: ylc
 * @Description:
 */
public class Util {
    public static final String[] C = {
            "0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "A", "B", "C", "D", "E", "F"
    };
    /**
     *  Byte转Bit
     * @param b
     * @return
     */
    public static String byteToBit(byte b) {
        return "" +(byte)((b >> 7) & 0x1) +
                (byte)((b >> 6) & 0x1) +
                (byte)((b >> 5) & 0x1) +
                (byte)((b >> 4) & 0x1) +
                (byte)((b >> 3) & 0x1) +
                (byte)((b >> 2) & 0x1) +
                (byte)((b >> 1) & 0x1) +
                (byte)((b >> 0) & 0x1);
    }
    /**
     *  Byte转HEX
     * @param b
     * @return
     */
    public static String byteToHEX(byte b) {
        int h = (b >> 4) & 0xf;
        int l = b & 0xf;
        String hex = C[h] + C[l];
        return hex;
    }

    /**
     * 拼接TLV
     * @param T
     * @param L
     * @param V
     * @return
     */
    public byte[] spliceArr(byte[] T, byte[] L, byte[] V){
        int len1 = T.length;
        int len2 = L.length;
        int len3 = V.length;
        byte[] TLV = new byte[len1 + len2 + len3];
        for (int i = 0; i < len1; i++) {
            TLV[i] = T[i];
        }
        for (int i = 0; i < len2; i++) {
            TLV[i+len1] = L[i];
        }
        for (int i = 0; i < len3; i++) {
            TLV[i+len1+len2] = V[i];
        }
        return TLV;
    }
    /**
     * 拼接两个字节数组
     * @param byte1 字节数组1
     * @param byte2 字节数组2
     * @return 返回拼接好的字节数组(字节数组1 + 字节数组2)
     */
    public static byte[] spliceArr(byte[] byte1, byte[] byte2) {
        byte[] byte3 = new byte[byte1.length + byte2.length];
        System.arraycopy(byte1, 0, byte3, 0, byte1.length);
        System.arraycopy(byte2, 0, byte3, byte1.length, byte2.length);
        return byte3;
    }

    /**
     * 将字符串转为字节数组
     * @param s 输入的字符串转换后为单个整型数
     * @return
     */
    public byte[] toByteArray(String s){
        int num = Integer.parseInt(s);
        byte[] bytes;
        if(num <= pow(2,8)){
            bytes = new byte[1];
        }else if(num <= pow(2,16)){
            bytes = new byte[2];
        }else if(num <= pow(2,24)){
            bytes = new byte[3];
        }else{
            bytes = new byte[4];
        }
        int temp = 0;
        for (int i = bytes.length - 1; i > -1; i--) {
            bytes[i] = (byte)((num >> temp) & 0xFF);
            temp += 8;
        }
        return bytes;
    }
    /**
     * 将整型转为字节数组
     * @param num
     * @return
     */
    public byte[] toByteArray(int num){
        byte[] bytes;
        if(num <= pow(2,8)){
            bytes = new byte[1];
        }else if(num <= pow(2,16)){
            bytes = new byte[2];
        }else if(num <= pow(2,24)){
            bytes = new byte[3];
        }else{
            bytes = new byte[4];
        }
        int temp = 0;
        for (int i = bytes.length - 1; i > -1; i--) {
            bytes[i] = (byte)((num >> temp) & 0xFF);
            temp += 8;
        }
        return bytes;
    }
    /**
     * 将byte[]转换为int
     * @param data 字节数据
     * @return 转换后的整型
     */
    public static int bytesToInt(byte[] data) {
        byte[] bytes = {0, 0, 0, 0};
        System.arraycopy(data, 0, bytes, (4 - data.length), data.length);
        return bytes[0] << 24 | (bytes[1] & 0xff) << 16 | (bytes[2] & 0xff) << 8 | (bytes[3] & 0xff);
    }

    /**
     * byte[]转String
     * @param b
     * @return
     */
    public String bytesToSring(byte[] b){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            sb.append((char)b[i]);
        }
        return sb.toString();
    }

    /**
     * 判断L值是否要扩展
     * @param length
     * @return
     */
    public byte[] exLength(int length){
        byte[] bytes = null;
        if(length >= 127){
            //长度扩展
            int exlen = length / 255;
            bytes = new byte[exlen + 1];
            byte exlenbyte = (byte)(exlen & 0xFF);
            bytes[0] = (byte)(129 & exlenbyte);
            byte[] temp = toByteArray(length);
            for (int i = 1; i < temp.length; i++) {
                bytes[i] = temp[i-1];
            }
        }else{
            bytes = new byte[]{(byte)length};
        }
        return bytes;
    }

    /**
     * 获取类型对应的值
     * @param s
     * @param type
     * @return
     */
    public int getTypeIndex(String s, String[] type){
        for (int i = 0; i < type.length; i++) {
            if(type[i].equals(s)){
                return i;
            }
        }
        return -1;
    }

    public static void showPacket(byte[] data) {
        bytesToHEX(data);
        System.out.println();
        bytesToBinary(data);
    }
    public static void bytesToBinary(byte[] data) {

        for (int i = 0; i < data.length; i++) {
            if(i %14 == 0){
                System.out.println();
            }
            System.out.print(byteToBit(data[i]));
            System.out.print(" ");
        }
    }
    public static void bytesToHEX(byte[] data) {
        for (int i = 0; i < data.length; i++) {
            if(i %14 == 0){
                System.out.println();
            }
            System.out.print(byteToHEX(data[i]));
            System.out.print("       ");
        }
    }
    /**
     * 生成一个100以内的随机数
     * @return 返回随机数
     */
    public static String getNum() {
        Random rd = new Random();
        int r = rd.nextInt(100);
        return String.valueOf(r);
    }
}
